import pymysql


def conn():
    #连接数据库
    con = pymysql.connect(host='192.168.1.240', port=3306, user='root', password='123456', db='pnp_test_only',
                          charset='utf8')
    try:
        #开启游标功能，创建游标对象并重命名
        with con.cursor() as cur:
            #执行SQL语句
            res = cur.execute('insert into assistant_version(ver,barid) values("ver","100125")');
            if res == 1:
                print('success')
            #提交事务，update,delete,insert等修改表中数据的需要commit，create,drop,alter等修改表结构的,就不需要commit,因为内部隐藏了commit
            con.commit()
    except pymysql.MySQLError as error:
        print(error)
        #运行失败就回滚
        con.rollback()
    finally:
        #断开数据库，释放资源
        con.close()


if __name__ == '__main__':
    conn()
